//
//  VoiceReadFooterView.m
//  ShowYork
//
//  Created by hxh on 2021/4/19.
//  Copyright © 2021 hxh. All rights reserved.
//

#import "VoiceReadFooterView.h"
#import "SKAutoScrollLabel.h"
#import "UIImageView+LBBlurredImage.h"
#import "ScrollLabelView.h"
@interface VoiceReadFooterView ()
@property (nonatomic,strong)ScrollLabelView *scrollLabel;
@property (nonatomic,strong)UIImageView *icon;
@property (nonatomic,strong)UILabel *dateLb;
@property (nonatomic,strong)UIImageView *statusIcon;
@property (nonatomic,strong)UIImageView *deleteIcon;
@property (nonatomic,strong)UIButton *deleteButton;
@property (nonatomic,strong)UIImageView *bgView;
@end

@implementation VoiceReadFooterView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setUI];
    }
    return self;
}

- (void)setUI{
    [self scrollAnimation];
}

- (void)show{
    
}

- (void)scrollAnimation {
    
    self.frame = CGRectMake(17,kScreenH - (IPHONE_X?34.0:16.0f) - 52, kScreenW - 32, 52);
    self.backgroundColor = [UIColor clearColor];
    
    self.bgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0,self.cl_width, self.cl_height)];
    self.bgView.layer.cornerRadius = 8.0f;
    self.bgView.contentMode = UIViewContentModeScaleAspectFill;
    self.bgView.layer.masksToBounds = YES;
    [self addSubview:self.bgView];
   
    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *effectview = [[UIVisualEffectView alloc] initWithEffect:blur];
    effectview.frame = self.bgView.frame;
    effectview.alpha = 1;
    [self.bgView addSubview:effectview];

    UIView *a = [[UIView alloc]initWithFrame:self.bgView.frame];
    a.backgroundColor = RGBA(255, 255, 255, 0.7);
    [self.bgView addSubview:a];
    
    self.icon = [[UIImageView alloc]initWithFrame:CGRectMake(6, 8, 27, 36)];
    self.icon.contentMode = UIViewContentModeScaleAspectFill;
    self.icon.layer.cornerRadius = 4;
    self.icon.layer.masksToBounds = YES;
    
    [self addSubview:self.icon];
    

    self.scrollLabel = [[ScrollLabelView alloc]initWithFrame:CGRectMake(45, 9, self.cl_width - 45 - 50, 16)];
    self.scrollLabel.textColor = C51;
    self.scrollLabel.textFont = [UIFont boldSystemFontOfSize:14];
    [self addSubview:self.scrollLabel];
        
    self.dateLb = [[UILabel alloc]init];
    self.dateLb.text = @"2:20 / 16:07";
    self.dateLb.textColor = C102;
    self.dateLb.font = font(10);
    [self addSubview:self.dateLb];

    self.dateLb.frame = CGRectMake(45, CGRectGetMaxY(self.scrollLabel.frame) + 4,100, 14);

    self.statusIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"voice_start_tag"]];
    self.statusIcon.highlightedImage = [UIImage imageNamed:@"voice_stop_tag"];
    self.statusIcon.cl_right = self.cl_width - 16;
    self.statusIcon.cl_y = 16;
    [self addSubview:self.statusIcon];

    self.deleteIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"voice_delete_tag"]];
    self.deleteIcon.cl_y = 20;

    self.deleteIcon.hidden = YES;
    [self addSubview:self.deleteIcon];

    self.deleteButton = [[UIButton alloc]init];
    [self.deleteButton addTarget:self action:@selector(deleteMethod) forControlEvents:UIControlEventTouchUpInside];
    self.deleteButton.hidden = YES;
    self.deleteButton.frame = CGRectMake(0, 0, 44, self.cl_height);
    [self addSubview:self.deleteButton];

    self.statusIcon.userInteractionEnabled = YES;

    UITapGestureRecognizer *stopTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(stopMethod)];
    [self.statusIcon addGestureRecognizer:stopTap];
    
}
- (void)linearAnimation {
    
    [self.scrollLabel continueScrollView];
}

- (void)stopAnimation{
    [self.scrollLabel stopScrollView];
}

- (void)deleteMethod{
    
    self.hidden = YES;

    self.statusIcon.highlighted = NO;
    
    [self.scrollLabel stopScrollView];
 
    self.deleteIcon.cl_x = self.statusIcon.highlighted?16:-16;
    self.icon.cl_x = self.statusIcon.highlighted?44:6;
    self.scrollLabel.cl_x = CGRectGetMaxX(self.icon.frame) + 12;
    self.scrollLabel.cl_width = self.cl_width - 50 - self.scrollLabel.cl_x;
    self.dateLb.cl_x = self.scrollLabel.cl_x;
    
    self.deleteButton.hidden = YES;
    self.deleteIcon.hidden = YES;
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(footerViewDeleteWithCurrentModel:)]) {
        [self.delegate footerViewDeleteWithCurrentModel:self.itemModel];
    }
    
}

- (void)stopMethod{
    
    self.statusIcon.highlighted = !self.statusIcon.highlighted;
    
    if (self.statusIcon.highlighted) {
        
        [self.scrollLabel stopScrollView];
        
        self.itemModel.status = XHPlayerListItemPause;
        if (self.delegate && [self.delegate respondsToSelector:@selector(footerViewPauseWithCurrentModel:)]) {
            [self.delegate footerViewPauseWithCurrentModel:self.itemModel];
        }
    }else{
        [self.scrollLabel continueScrollView];
        self.itemModel.status = XHPlayerListItemPlay;
        if (self.delegate && [self.delegate respondsToSelector:@selector(footerViewPlayWithCurrentModel:)]) {
            [self.delegate footerViewPlayWithCurrentModel:self.itemModel];
        }
        
    }
    
    [UIView animateWithDuration:0.25 animations:^{
        self.deleteIcon.cl_x = self.statusIcon.highlighted?16:-16;
        self.icon.cl_x = self.statusIcon.highlighted?44:6;
        self.scrollLabel.cl_x = CGRectGetMaxX(self.icon.frame) + 12;
        self.dateLb.cl_x = self.scrollLabel.cl_x;
    } completion:^(BOOL finished) {
        if (finished) {
            self.deleteIcon.hidden = !self.statusIcon.highlighted;
            self.deleteButton.hidden = !self.statusIcon.highlighted;
        }
    }];
}
- (void)setTitle:(NSString *)title{
    self.scrollLabel.text = title;
}

- (void)setCoverUrl:(NSString *)coverUrl{
    [self.bgView sd_setImageWithURL:[NSURL URLWithString:coverUrl] completed:nil];
    [self.icon sd_setImageWithURL:[NSURL URLWithString:coverUrl]];
}
- (void)setItemModel:(VoiceReadPlayerItemModel *)itemModel{
    
    _itemModel = itemModel;
    
    if (itemModel.status == XHPlayerListItemPlay ||
        itemModel.status == XHPlayerListItemReady) {
        self.statusIcon.highlighted = NO;
    }else{
        self.statusIcon.highlighted = YES;
    }
    [UIView animateWithDuration:0.25 animations:^{
        self.deleteIcon.cl_x = self.statusIcon.highlighted?16:-16;
        self.icon.cl_x = self.statusIcon.highlighted?44:6;
        self.scrollLabel.cl_x = CGRectGetMaxX(self.icon.frame) + 12;
        self.dateLb.cl_x = self.scrollLabel.cl_x;
    } completion:^(BOOL finished) {

    }];
    self.deleteIcon.hidden = !self.statusIcon.highlighted;
    self.deleteButton.hidden = !self.statusIcon.highlighted;
    
    
    int currentTime = itemModel.currentTime;
    int totalTime = itemModel.voiceTime.intValue/1000;
    

    NSString *strShowTime = [NSString stringWithFormat:@"%d:%02d / %d:%02d",currentTime/60,currentTime%60,totalTime/60,totalTime%60];
    self.dateLb.text = strShowTime;
}

@end
